<template>
	<view>
		<view class="date" :style="`--dc:${color}`">
			<view class="date_body">
				<view class="date_body_item" v-for="(item, index) in data" :key="index" @click="change(item)"
					:class="item.day == today ? 'action':''">
					<view class="date_body_item_week">
						{{item.week}}
					</view>
					<view class="date_body_item_day" :class="item.week == '六' || item.week == '日' ? 'gray':''">
						{{item.day}}
					</view>
				</view>
			</view>

			<view class="date_icons" @click="open">
				<view class="date_icons_month">
					{{month}}月
				</view>
				<uni-icons type="calendar" size="30" color="var(--dc)"></uni-icons>
			</view>

		</view>
		<uni-calendar ref="calendarRef" :clear-date="true" :endDate="current" :insert="false" @confirm="confirm"
			@close="close" />
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue"

	let props = defineProps({
		current: String,
		color: {
			type: String,
			default: "#f00"
		}
	})
	let calendarRef = ref(null)

	let data = ref([]);
	let month = ref("")
	let today = ref("");
	let year = ref("")

	setTimeout(_ => {
		create(props.current || new Date());
	}, 10)
	const create = (date) => {
		data.value = [];
		let weeks = ['一', '二', '三', '四', '五', '六', '日'];
		let d = new Date(date);
		let week = d.getDay();
		let diff = d.getDate() - week + (week === 0 ? -6 : 1);
		year.value = d.getFullYear();
		month.value = d.getMonth() + 1;
		today.value = d.getDate();

		let last_month = new Date(year.value, month.value, 0).getDate();
		let this_month = new Date(year.value, month.value + 1, 0).getDate();
		for (let i = 0; i < 7; i++) {
			let day = diff;
			if (diff < 1) {
				day = last_month + day;
			}
			if (diff >= this_month) {
				diff = 0
			}
			data.value.push({
				week: weeks[i],
				day: day
			})
			diff++;
		}
	}

	const open = () => {
		if (calendarRef.value) calendarRef.value.open()
	}

	const confirm = e => {
		create(e.fulldate)
		emits('change', e.fulldate)
	}

	const close = () => {}

	const emits = defineEmits(['change'])
	const change = (e) => {
		today.value = e.day;
		emits('change', `${year.value}-${month.value}-${today.value}`)
	}
</script>

<style scoped lang="scss">
	.gray {
		color: #999;
	}

	.action {
		.date_body_item_week {
			color: var(--dc);
		}

		.date_body_item_day {
			background: var(--dc);
			color: #fff;
		}
	}

	.date {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #fff;
		// box-shadow: 0 0 20rpx rgba(0, 0, 0, .06);
		margin: 20rpx 20rpx 0;
		border-radius: 20rpx;

		&_icons {
			width: 140rpx;
			height: 140rpx;
			text-align: center;
			border-left: 4rpx solid #eee;

			&_month {
				font-size: 32rpx;
				margin-top: 20rpx;
			}
		}

		&_body {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: calc(100% - 120rpx);
			padding: 20rpx 30rpx;

			&_item {
				text-align: center;

				&_week {
					color: #999;
					font-size: 24rpx;
				}

				&_day {
					width: 55rpx;
					height: 55rpx;
					line-height: 55rpx;
					border-radius: 10rpx;
					margin: 5rpx auto 0;
					font-size: 32rpx;
				}
			}
		}
	}
</style>